#ifndef EXCEPTION_H
#define EXCEPTION_H

typedef struct faultRegion faultRegion;
faultRegion* faultRegions;


struct faultRegion
{
	uint32_t faultHandler;
	uint32_t regionStart;
	void* prevRegion;
	void* nextRegion;
};


faultRegion* getCurrentFaultRegion()
{
	faultRegion* fault = faultRegions;
	while(fault->nextRegion != (faultRegion*)0)
	{
		fault = (faultRegion*)(fault->nextRegion);
	}
	return fault;
}

void throw(char* exception)
{
	if(getCurrentFaultRegion()->faultHandler != 0)
	{
		pushDword(exception);
		pos = getCurrentFaultRegion()->faultHandler;
	}
	else
	{
		printf("*** FATAL: Unhandled exception!\n");
		printf("Exception '%s' was thrown!\n",exception);
		printTrace();
		exit(1);
	}
}

void printTrace()
{	
	int32_t i = 1;
	printf("\nCall Trace\n");
	for(i; i < CallStackPointer; i+=2)
	{
		if(theProgram != (void*)0)
			printf("	[<0x%08x>] %s\n",CallStack[i],getSymName(theProgram,CallStack[i]));
		else
			printf("	at 0x%08x\n",CallStack[i]);
	}
	printf("\n");
	dumpRam();
}

#endif
